package cn.johnyu.service;

import cn.johnyu.domain.DomainUser;
import cn.johnyu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        DomainUser domainUser = userMapper.selectUserByUsername(username);
        if(domainUser==null){
            throw new UsernameNotFoundException("找不到用户");
        }
        UserDetails user = User.withUsername(domainUser.getUsername())
                .password(domainUser.getPassword())
                .authorities(domainUser.getRoles().toArray(new String[0]))
                .build();
        return user;
    }
}
